<?php
	defined('IN_TS') or die('Access Denied.');
	
	$userid = aac('user')->isLogin();
	
	
	switch($ts){
		//基本信息
		case "base":
			$giftid = intval($_GET['giftid']);
			$strGift = $new['gift']->find('gift',array(
				'giftid'=>$giftid,
			));
			$strGift['content'] = tsDecode($strGift['content']);

			if ($strGift ['addtime'] < strtotime('-7 day') && $TS_USER ['isadmin'] == 0) {
				tsNotice('超过一周，不允许编辑，如需代为编辑请给微澜图书馆公众号后台留言');
			}
			
			if($strGift['userid']!=$userid && $GLOBALS['TS_USER']['isadmin']==0){
				tsNotice('非法操作！');
			}
			
			$title = '编辑商品信息';
			include template("edit_base");
			break;
		
		case "basedo":

			$giftid = intval($_POST['giftid']);

            $strGift = $new['gift']->find('gift',array(
                'giftid'=>$giftid,
            ));


            if($strGift['userid']!=$userid && $GLOBALS['TS_USER']['isadmin']==0){
                tsNotice('非法操作！');
            }


			$title = trim($_POST['title']);
			$cate = intval($_POST['cate']);
			$typeid = intval($_POST['typeid']);
			$limittime = trim($_POST['limittime']);
			$province = trim($_POST['province']);
			$city = trim($_POST['city']);
			$address = trim($_POST['address']);
			$transfer = trim($_POST['transfer']);
			$content = tsClean($_POST['content']);
			$price = trim($_POST['price']);
			$quota = trim($_POST['quota']);
			$photo = trim($_POST['photo']);

			$limittime = trim(str_replace('AM','',$limittime));
			$limittime = trim(str_replace('PM','',$limittime));
			$limittime = strtotime($limittime);


			if($limittime==''){
				tsNotice('设一个期限呗');
			}


			if($quota==''){
				tsNotice('数目不能为空');
			}


			if($title == '' || $content == ''){
				tsNotice('标题和内容不能为空');
			}

			if($province == '' || $city == '' ){
				tsNotice('省和市必须填写');
			}

            if (intval($TS_USER['isadmin']) == 0) {
                // 过滤内容开始
                aac('system') -> antiWord($title);
                aac('system') -> antiWord($content);
                // 过滤内容结束
            }

			if($photo==''){
				tsNotice('必须上传封面照片！');
			};
			$img_path = $photo;

			// $img_path = getTextPhotos(tsDecode($content));
			// if($img_path==''){
			// 	tsNotice('请至少插入一张图片作为封面！');
			// };
			// $count = count($img_path);
			// $img_path = $img_path[$count-1];
			//$url=preg_replace('#/$#','',SITE_URL);
			//$img_path = $url.$img_path;

			//更新数据
			$new['gift']->update('gift',array(
				'giftid'=>$giftid,
			),array(
				'title'	=> $title,
				'typeid' => $typeid,
				'cate' => $cate,
				'limittime' => $limittime,
				'address'	=> $address,
				'province'	=> $province,
				'city'  => $city,
				'content' => $content,
				'price'=>$price,
				// 'isaudit'=>$isaudit,
				'quota'=>$quota,
				'transfer'=>$transfer,
				'path'=> dirname($img_path),
				'photo'=> $img_path,
			));
			
			//上传
			// $arrUpload = tsUploadPhotoUrl($img_path,$giftid,'gift',array('jpg','gif','png'));
			
			// if($arrUpload){
			// 	$new['gift']->update('gift',array(
			// 		'giftid'=>$giftid,
			// 	),array(
			// 		'path'=>$arrUpload['path'],
			// 		'photo'=>$arrUpload['url'],
			// 	));
            //     //删除缓存图片
			// 	tsDimg ( $arrUpload ['url'], 'gift', '100', '', $arrUpload ['path'] );
			// 	tsDimg ( $arrUpload ['url'], 'gift', '200', '', $arrUpload ['path'] );
            //     tsDimg ( $arrUpload ['url'], 'gift', '300', '', $arrUpload ['path'] );
            //     tsDimg ( $arrUpload ['url'], 'gift', '500', '', $arrUpload ['path'] );
			// }
			
			header("Location: ".tsUrl('gift','show',array('id'=>$giftid)));
		
			break;
			
		case "join":
			
			$giftid = intval($_GET['giftid']);
			$strGift = $new['gift']->find('gift',array(
				'giftid'=>$giftid,
			));

            if($strGift['userid']!=$userid && $GLOBALS['TS_USER']['isadmin']==0){
                tsNotice('非法操作！');
            }
			
			$arrUser = $new['gift']->findAll('gift_users',array(
				'giftid'=>$giftid,
			),'addtime desc');
			
			
			$title = '订单';
			include template('edit_join');
			
			break;

	}